package com.zhangzhan.blog_service.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.base.common_model.entity.User;
import com.zhangzhan.base.common_model.result.ResponseResult;
import com.zhangzhan.base.common_model.service.UserService;
import com.zhangzhan.base.enums.AppHttpCodeEnum;
import com.zhangzhan.base.exception.SystemException;
import com.zhangzhan.base.utils.CopyBean;
import com.zhangzhan.blog_model.entity.Comment;
import com.zhangzhan.blog_model.vos.CommentListVo;
import com.zhangzhan.blog_model.vos.PageVo;
import com.zhangzhan.blog_service.mapper.CommentMapper;
import com.zhangzhan.blog_service.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

import static com.zhangzhan.base.constant.StatusCode.ROOT_ID;
import static com.zhangzhan.base.constant.StatusCode.TYPE_PL;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author zhangzhan
 * @since 2024-02-05
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserService userService;

    /**
     * @param articleId
     * @param pageNum
     * @param pageSize
     * @return com.zhangzhan.base.common_model.result.ResponseResult<java.util.List < com.zhangzhan.blog_model.vos.PageVo>>
     * @description 展示评论内容
     * @author zhangyiheng
     * @date 2024/2/5 22:23
     */
    @Override
    public ResponseResult<List<PageVo>> commentList(Long articleId, Long pageNum, Long pageSize) {

        //先查询出文章下的根评论
        //分页
        IPage<Comment> page = new Page<>(pageNum, pageSize);
        page(page, Wrappers.<Comment>lambdaQuery()
                .eq(Comment::getArticleId, articleId)
                .eq(Comment::getRootId, ROOT_ID)
                .eq(Comment::getType, TYPE_PL));

        List<Comment> comments = page.getRecords();
        PageVo pageVo = new PageVo();

        if (CollUtil.isNotEmpty(comments)) {
            List<CommentListVo> commentListVos = CopyBean.copyBeanList(comments, CommentListVo.class);

            for (CommentListVo vo : commentListVos) {


                Long id = vo.getId();
                List<Comment> childComment = this.list(Wrappers.<Comment>lambdaQuery()
                        .eq(Comment::getRootId, id)
                        .eq(Comment::getType, TYPE_PL)
                        .orderByDesc(Comment::getCreateTime));

                List<CommentListVo> childCommentVo = null;

                if (CollUtil.isNotEmpty(childComment)) {
                    childCommentVo = CopyBean.copyBeanList(childComment, CommentListVo.class);
                    childCommentVo.forEach(po -> {
                        //赋值子评论是哪个用户回复的
                        po.setUsername(getNikeName(po.getCreateBy()));
                        //赋值子评论回复了哪个用户
                        if (po.getToCommentUserId() != -1) {
                            po.setToCommentUserName(getNikeName(po.getToCommentUserId()));
                        }
                    });
                    //赋值子评论
                    vo.setChildren(childCommentVo);
                }
                vo.setUsername(getNikeName(vo.getCreateBy()));
            }


            pageVo.setRows(commentListVos);
            pageVo.setTotal(page.getTotal());
            return ResponseResult.okResult(pageVo);
        }
        return null;
    }

    private String getNikeName(Long userId) {
        return Optional.ofNullable(userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getId, userId)).getNickName()).orElse("");
    }


    /**
     * @param comment
     * @return com.zhangzhan.base.common_model.result.ResponseResult
     * @description 用户评论接口
     * @author zhangyiheng
     * @date 2024/2/8 21:01
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        //判断评论内容不能为空
        if (StrUtil.isBlank(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        //添加评论表
        this.save(comment);

        return ResponseResult.okResult();
    }
}
